﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using bmapi.HelperClass;
using bmapi.Models.DataEntity;
using static bmapi.Models.ShowModel;

namespace bmapi.Controllers
{
    public class FtpAmendController : BuyiController
    {
        [HttpGet,Route("FtpAmend/GetFtp")]
        public HttpResponseMessage GetFtp()
        {
            try
            {
                SQLCommand sc = null;
                sc = new SQLCommand("select * from system_tb where key_name='FtpPassword'");
                DataRow dataRow = sc.ExecuteOneRow();
                if (dataRow == null)
                {
                    sc = new SQLCommand("insert into system_tb(key_name,key_val) values('FtpPassword','BuyiPassword')");
                    sc.ExecuteNonQuery();
                }                    
                sc = new SQLCommand("SELECT key_val FROM System_tb where key_name='FtpPassword'");
                DataRow dr = sc.ExecuteOneRow();
                string ftpkeyval = dr["key_val"].ToString();
                return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = 1, rows = 1, ftp = new { username = "BuyiMesUser", newpassword = ftpkeyval } } });
            }
            catch (Exception e)
            {
                WriteLog("FtpAmend-get",e.Message);
                throw;
            }           
        }

        [HttpGet, Route("FtpAmend/GetDebug")]
        public HttpResponseMessage GetDebug()
        {
            try
            {
                SQLCommand sc = new SQLCommand("SELECT key_val FROM System_tb where key_name='WriteDebug'");
                DataRow dr = sc.ExecuteOneRow();
                if (dr == null)
                {
                    sc = new SQLCommand("insert into system_tb(key_name,key_val) values('WriteDebug','y')");
                    sc.ExecuteNonQuery();
                }
                sc = new SQLCommand("SELECT key_val FROM System_tb where key_name='WriteDebug'");
                dr = sc.ExecuteOneRow();
                string bugkeyval = dr["key_val"].ToString();
                if (bugkeyval == null)
                    return AssistFun.toJson(new RespondModel(201,"无日志"));
                return AssistFun.toJson(new { status = 200, tip = "成功", data = new { total = 1, rows = 1, ftp = new { username = "BuyiMesUser", bug = bugkeyval } } });
            }
            catch (Exception e)
            {
                WriteLog("FtpAmend-get", e.Message);
                throw;
            }
        }

        [HttpPut,Route("FtpAmend/updateftppwd")]
        public HttpResponseMessage PutPassWord(dynamic obj)
        { 
            string pwd = obj.newpassword.ToString();
            SQLCommand sc = new SQLCommand("UPDATE System_tb SET key_val=@newpassword where key_name='FtpPassword'", "@newpassword", pwd);
            int count = sc.ExecuteNonQuery();
            return FtpRespond(count);
        }

        [HttpPut,Route("FtpAmend/updatebugflg")]
        public HttpResponseMessage PutDebug(dynamic obj)
        {
            string bug = obj.bug.ToString();
            SQLCommand sc = new SQLCommand("UPDATE System_tb SET key_val=@bug where key_name='WriteDebug'", "@bug", bug);
            int count = sc.ExecuteNonQuery();
            return  FtpRespond(count);
        }
        /// <summary>
        /// 设置erp数据访问路径
        /// </summary>
        /// <param name="obj"></param>
        [HttpPut,Route("seturl")]
        public HttpResponseMessage SetRequstPara(dynamic obj)
        {
            try
            {
                if (obj == null)
                    return Request.CreateResponse(new { status = 201, tip = "请提供数据" });
                else
                {
                    string url = obj.url.ToString();
                    if (!string.IsNullOrEmpty(url) || url.Trim().Length > 0)
                    {
                        SQLCommand sc = new SQLCommand("select key_val from System_tb where key_name='Url'");
                        if (sc.ExecuteScalar() != null)
                        {
                            sc = new SQLCommand("delete from System_tb where key_name='Url'");
                            if (sc.ExecuteNonQuery() > 0)
                                return AddUrl(url);
                            else
                               return Request.CreateResponse(new { status = 201, tip = "操作失败" });
                        }
                        else
                            return AddUrl(url);
                    }
                    else
                        return Request.CreateResponse(new { status = 201, tip = "路径不能为空" });
                }
            }
            catch (Exception ex)
            {
               return Request.CreateResponse(new { status = 201, tip = "异常："+ex.Message });
            }

        }
        /// <summary>
        /// 设置erp订单数据请求路径
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private HttpResponseMessage AddUrl(string url)
        {
            try
            {
                SQLCommand sc = new SQLCommand("insert into System_tb values('Url',@urlval)", "@urlval", url);
                if (sc.ExecuteNonQuery() > 0)
                   return Request.CreateResponse(new { status = 200, tip = "操作成功" });
                else
                    return Request.CreateResponse(new { status = 201, tip = "操作失败" });
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(new { status = 201, tip = "异常："+ex.Message });
            }
           
        }

        private HttpResponseMessage FtpRespond(int count)
        {
            if (count > 0)
                return AssistFun.toJson(new RespondModel(200, "修改成功"));
            else
                return AssistFun.toJson(new RespondModel(201, "修改失败"));
        }
    }
}
